package com.coffee_shop_app.module;


import android.util.Log;

import com.coffee_shop_app.common.vo.WebsocketVo;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;
import com.google.gson.Gson;

import java.util.Map;
import java.util.HashMap;

import javax.annotation.Nullable;

public class ToastModule extends ReactContextBaseJavaModule {
    private static ReactApplicationContext reactContext;

    public ToastModule(ReactApplicationContext context) {
            super(context);
            reactContext = context;
    }
    @Override
    public String getName() {
        return "ToastExample";
    }
    @Override
    public Map<String, Object> getConstants() {
        Log.i("TAG", "isam2017");
        final Map<String, Object> constants = new HashMap<>();
//        constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
//        constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
        return constants;
    }
    private void sendEvent(ReactContext reactContext,
                           String eventName,
                           @Nullable WritableMap params) {
        reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, params);
    }
    @ReactMethod
    public void addListener(String eventName) {
        // Set up any upstream listeners or background tasks as necessary
    }
    @ReactMethod
    public void removeListeners(Integer count) {
        // Remove upstream listeners, stop unnecessary background tasks
    }
    /**
     * 发往前端数据
     */
    public void sendFEMessage(WebsocketVo websocketVo) {
        System.out.println("我爱你中国");
        WritableMap params = Arguments.createMap();
        Gson gson = new Gson();
        params.putString("eventProperty", gson.toJson(websocketVo));
        sendEvent(reactContext, "EventReminder", params);
    }
}